﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YooAsset;

public class ReferenceObj
{
    public ReferenceObj(AssetOperationHandle handle)
    {
        this.handle = handle;
        Reference();
    }
    private AssetOperationHandle handle;
    /// <summary>
    /// 获取对象
    /// </summary>
    /// <returns></returns>
    public UnityEngine.Object GetObject()
    {
        Reference();
        return handle.AssetObject;
    }
    private int refCount = 0;
    /// <summary>
    /// 引用计数+1
    /// </summary>
    public void Reference()
    {
        refCount += 1;
    }
    /// <summary>
    /// 引用计数-1
    /// </summary>
    public void Release()
    { 
        refCount -= 1;
    }
    /// <summary>
    /// 强制销毁
    /// </summary>
    public void ForcedDestroy()
    {
        refCount = 0;
        CheckDestroy();
    }
    /// <summary>
    /// 检测销毁
    /// </summary>
    public bool CheckDestroy()
    {
        if(refCount <= 0)
        {
            handle.Release();
            handle = null;
            return true;
        }
        return false;
    }
    public bool IsDestroy()
    { 
        return refCount <= 0;
    }
    public override string ToString()
    {
        string addresData = this.handle == null ? "obj：Null" : "obj：" + this.handle.AssetObject.name + "，引用计数：" + refCount+"\n" ;
        return addresData;
    }
}
